/*
	业务拓扑图Ember组件
*/
define(
	[
		'require',
		'./topo'
	],
	function (require) {
		"use strict";

		var BusinessTopo = require('./topo');

		//业务拓扑视图
		var BusinessTopoView = Ember.Component.extend({
			scaling: 1,

			didInsertElement: function(){
				var view = this;

				this.initChart();



				APM.on("mainContainerResize", this, "_resizeChart");
				APM.on("topoContainerResize", this, "_resizeChart");
			},

			willDestroyElement: function(){
				APM.off("mainContainerResize");
				APM.off("topoContainerResize");
				this.chart.destroy();
			},

			initChart: function(){
				var view = this;
				var content = this.get("content");
				var dom = this.$()[0];
				var performances = this.get("performances");

				var businessTopo = BusinessTopo.create({
					container: dom,
					ctrl: this.get("controller"),
					data: content,
					auth: this.get("auth"),
					timePoint: this.timePoint,
					menusConfig: this.menusConfig,
					onMenuClick: function(menuCode, component){
						return view.sendAction("onMenuClick", menuCode, component.data, view);
					},
					dropNodeToNode: function(draggingNode, droppingNode){
						return view.sendAction("onDropNodeToNode",draggingNode, droppingNode, view);
					},
					onNodeStackClick: function(node){
						return view.sendAction("onNodeStackClick", node, view);
					},
					onNodePfmsClick: function(node, dataKey){
						return view.sendAction("onNodePfmsClick", node, dataKey, view);
					},
					onScaled: function(scaling){
						view.set("scaling", scaling);
						return view.sendAction("onScaled", scaling, view);
					}
				});
				businessTopo.render();

				if(content && performances){
					businessTopo.updatePerformances(performances);
				}

				this.chart = businessTopo;
			},

			contentChange: Ember.observer("content", function(){
				this.chart&&this.chart.set("data", this.get("content"));
			}),

			timePointChange: Ember.observer("timePoint", function(){
				if(this.chart){
					this.chart.set("timePoint", this.get("timePoint"));
				}
			}),

			updatePerformances: function(performances){
				if(this.chart){
					this.chart.updatePerformances(performances);
				}
			},

			setDisplayMode: function(size){
				this.chart.setDisplayMode(size);
			},

			zoomIn: function(){
				var scaling = this.scaling+0.1;
				this.chart.scale(scaling, this.centerX, this.centerY);
			},

			zoomRestore: function(){
				var scaling = 1;
				this.chart.scale(scaling, this.centerX, this.centerY);
			},

			zoomOut: function(){
				var scaling = Math.max(0.5, this.scaling-0.1);
				this.chart.scale(scaling, this.centerX, this.centerY);
			},

			fullScreen: function(){
				this.$().fullScreen();
			},

			toJSON: function(){
				return this.chart.toJSON();
			},

			addNodes: function(nodes, relations){
				return this.chart.addNodes(nodes, relations);
			},

			delNodes: function(nodes){
				this.chart.delNodes(nodes);
			},

			expandCluster: function(node){
				this.chart.expandCluster(node);
			},

			foldCluster: function(){
				this.chart.foldCluster(true);
			},

			addNodeToCluster: function(node, clusterNode){
				this.chart.addNodeToCluster(node, clusterNode);
			},

			removeNodeFromCluster: function(node){
				this.chart.removeNodeFromCluster(node);
			},

			changeDisplayModeOf: function(node){
				this.chart.changeDisplayModeOf(node);
			},

			setLinkNodeMode: function(flag){
				this.chart.setLinkNodeMode(flag);
			},

			updateNodeName: function(node){
				this.chart.updateHeader(node);
			},

			updateNodeIcon: function(node){
				this.chart.updateHeader(node);
			},

			updateHeader: function(node){
				this.chart.updateHeader(node);
			},

			removeLinkLine: function(data){
				this.chart.removeLinkLine(data);
			},

			_resizeChart: function(){
				var dom = this.$();
				this.centerX = $(dom).width()/2;
				this.centerY = $(dom).height()/2;
				//防止刷新太频繁
				var view = this;
				clearTimeout(this.resizeTimer);
				this.resizeTimer = setTimeout(function(){
					view.chart.resize();
				}, 17);
			}
		});

		return BusinessTopoView;
	}
);